package com.axzhengxin.platform.common.util;

import org.apache.commons.codec.binary.Hex;
import org.springframework.util.Base64Utils;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.security.Security;

/**
 * AesUtil
 * AES加解密工具类
 *
 * @author jiangnan
 * @date 2021/2/23
 */
public class AesUtil {

    private static final String ALGORITHM_AES = "AES";
    private static final String CIPHER_ALGORITHM_CBC = "AES/CBC/PKCS5Padding";
    private static final String BC_PROVIDER = "BC";
    private static final String DEFAULT_ROOT_IV = "0000000000000000";
    private static final Charset DEFAULT_CHARSET = StandardCharsets.UTF_8;

    static {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
    }

    /**
     * 使用字符串形式的秘钥，加密字符串形式的明文，返回字节数组形式的密文
     *
     * @param content 明文
     * @param key     秘钥
     * @return 密文字节数组
     * @throws Exception 可能抛出的异常
     */
    private static byte[] encrypt(String content, String key) throws Exception {
        return encrypt(toByteArray(content), decodeKey(key));
    }

    /**
     * 使用字符串形式的秘钥，加密字符串形式的明文，返回以Base64方式编码的字符串形式的密文
     *
     * @param content 明文
     * @param key     秘钥
     * @return 密文字符串，以Base64方式编码
     */
    public static String encryptToBase64String(String content, String key) {
        try {
            return Base64Utils.encodeToString(encrypt(content, key));
        } catch (Exception e) {
            throw new RuntimeException("加密过程中发生异常", e);
        }
    }

    /**
     * 使用字节数组形式的秘钥，加密字节数组形式的明文，返回字节数组形式的密文
     *
     * @param content 明文
     * @param key     秘钥
     * @return 密文
     * @throws Exception 可能抛出的异常
     */
    private static byte[] encrypt(byte[] content, byte[] key) throws Exception {
        return getCipher(Cipher.ENCRYPT_MODE, key).doFinal(content);
    }

    public static byte[] decrypt(String content, String key) throws Exception {
        return decrypt(toByteArray(content), decodeKey(key));
    }

    public static String decryptFromBase64String(String content, String key) {
        try {
            return new String(decrypt(Base64Utils.decodeFromString(content), decodeKey(key)), DEFAULT_CHARSET);
        } catch (Exception e) {
            throw new RuntimeException("解密过程中发生异常", e);
        }
    }

    private static byte[] decrypt(byte[] content, byte[] key) throws Exception {
        return getCipher(Cipher.DECRYPT_MODE, key).doFinal(content);
    }

    private static Cipher getCipher(int mode, byte[] keyArray) throws Exception {
        SecretKey key = new SecretKeySpec(keyArray, ALGORITHM_AES);
        return getCipher(mode, key);
    }

    private static Cipher getCipher(int mode, SecretKey key) throws Exception {
        Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM_CBC, BC_PROVIDER);
        cipher.init(mode, key, new IvParameterSpec(DEFAULT_ROOT_IV.getBytes()));
        return cipher;
    }

    private static byte[] toByteArray(String src) {
        return src.getBytes(DEFAULT_CHARSET);
    }

    private static byte[] decodeKey(String key) throws Exception {
        return Hex.decodeHex(key.toCharArray());
    }

}
